//
//  UITableView+EmptyData.m
//

#import "UITableView+EmptyData.h"

#import <objc/runtime.h>

#define DefaultTipImage @"icon_empty"

#define TipImageViewWidth 100
#define TipImageViewHeight 100
#define TipLabelHeight 25

@implementation UITableView (EmptyData)

#pragma mark - tipView
- (void)setTipView:(UIView *)tipView {
    objc_setAssociatedObject(self, @selector(tipView), tipView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)tipView {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - tipTopView
- (void)setTipTopView:(UIView *)tipTopView{
    objc_setAssociatedObject(self, @selector(tipTopView), tipTopView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)tipTopView {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - tipImageView
- (void)setTipImageView:(UIImageView *)tipImageView {
    objc_setAssociatedObject(self, @selector(tipImageView), tipImageView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImageView *)tipImageView {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - tipLabel
- (void)setTipLabel:(UILabel *)tipLabel {
    objc_setAssociatedObject(self, @selector(tipLabel), tipLabel,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UILabel *)tipLabel {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - startTip
- (void)setStartTip:(BOOL)startTip {
    if (startTip) {
        self.tipImage = [UIImage imageNamed:DefaultTipImage];
        if (self.tipView == nil) {
            [self setupViews];
        }
    }
    objc_setAssociatedObject(self, @selector(startTip), @(startTip),
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)startTip {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}


- (CGFloat)disFloat{
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setDisFloat:(CGFloat)disFloat{
    objc_setAssociatedObject(self, @selector(disFloat), @(disFloat),
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGFloat)floatY{
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFloatY:(CGFloat)floatY{
    objc_setAssociatedObject(self, @selector(floatY), @(floatY),
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)ls_layoutSubviews {
    [self ls_layoutSubviews];
    if (self.startTip) {

        self.tipView.frame = CGRectMake(0, 0,kScreenWidth , kScreenHeight);
        self.tipTopView.frame = CGRectMake(0, 0, kScreenWidth, 10);
        [self settingFrame];
        
    }
}

- (void)settingFrame {

    CGSize size = self.bounds.size;
    
    CGFloat imgWidth = self.tipImage.size.width ;
    CGFloat imgHeight = self.tipImage.size.height ;
    
    CGFloat btFloat = 10;
            
    CGFloat y = (kScreenHeight - imgHeight)/2 - kNavigationBarHeight - kGeneralHeight/2 - self.disFloat;
    
    self.tipImageView.frame = CGRectMake((kScreenWidth - imgWidth)/2, y, imgWidth, imgHeight);
        
    if (self.tipImage.size.width == 0) {
        self.tipLabel.frame = CGRectMake(0, kScreenHeight/2 - TipLabelHeight*2 - self.disFloat, size.width,TipLabelHeight);
    }else{
        self.tipLabel.frame = CGRectMake(0, CGRectGetMaxY(self.tipImageView.frame) + 10, size.width,TipLabelHeight);
    }
    
    
    self.tipLabel.frame = CGRectMake(0, CGRectGetMaxY(self.tipImageView.frame) + btFloat, size.width,TipLabelHeight);

}

- (void)setupViews {
    UIView *tipView = [[UIView alloc] init];
    tipView.backgroundColor = [UIColor whiteColor];
    
    self.tipView = tipView;
    [self addSubview:tipView];
    
    UIView *tipTopV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    tipTopV.backgroundColor = [UIColor clearColor];
    self.tipTopView  = tipTopV;
    
    
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.textColor = [UIColor colorWithHexString:@"#AAAAAA"];
    tipLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
    tipLabel.textAlignment = 1;
    [tipView addSubview:tipLabel];
    self.tipLabel = tipLabel;
    
    
    UIImageView *tipImageView = [[UIImageView alloc] init];
    tipImageView.contentMode = UIViewContentModeScaleAspectFit;
    
    [tipView addSubview:tipImageView];
    self.tipImageView = tipImageView;
    
    self.tipView.frame = CGRectMake(0, 0, self.frame.size.width, kScreenHeight);
    
    [self settingFrame];
}

#pragma mark - tipImage
- (void)setTipImage:(UIImage *)tipImage {
    self.tipImageView.image = tipImage;
    objc_setAssociatedObject(self, @selector(tipImage), tipImage,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImage *)tipImage {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - tipTitle
- (void)setTipTitle:(NSString *)tipTitle {
    
    self.tipLabel.text = tipTitle;
    objc_setAssociatedObject(self, @selector(tipTitle), tipTitle,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)tipTitle {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - tempTitle
- (void)setTempTitle:(NSString *)tempTitle {
    objc_setAssociatedObject(self, @selector(tempTitle), tempTitle,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)tempTitle {
    return objc_getAssociatedObject(self, _cmd);
}


@end

